﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace SimpleGameEngine
{
    public class GameSprite : GameObject
    {
        public int CurrentFrame;

        protected List<Bitmap> _frames = new List<Bitmap>();
        protected GameState _gameState;

        private Image sprite;
        private bool animated;
        private double animTimer;
        private double _elapsedTime;

        public GameSprite(GameState gameState, float x, float y, string filename)
        {
            //Load the bitmap
            _frames.Add(new Bitmap(filename));

            //Set the location and use the hieght and width from the 1st frame
            initialize(gameState, x, y, _frames[0].Width, _frames[0].Height);
        }

        public GameSprite(GameState gameState, float x, float y, string filename1, string filename2)
        {
            //Load the 2 animation frames
            _frames.Add(new Bitmap(filename1));
            _frames.Add(new Bitmap(filename2));

            //Set the location and use the hieght and width from the 1st frame
            initialize(gameState, x, y, _frames[0].Width, _frames[0].Height);
        }

        public GameSprite(GameState gameState, float x, float y, string filename, int frameheight)
        {
            animated = true;
            sprite = new Bitmap(filename);

            //Set the location and use the hieght and width from the 1st frame
            initialize(gameState, x, y, sprite.Width, frameheight);
        }

        private void initialize(GameState gameState, float x, float y, float width, float height)
        {
            _gameState = gameState;
            Location.X = x;
            Location.Y = y;
            Size.Width = width;
            Size.Height = height;
            CurrentFrame = 0;

            if (animated)
            {
                for (CurrentFrame = 0; CurrentFrame < (sprite.Height / Size.Height); CurrentFrame++)
                {
                    _frames.Add(new Bitmap(PaintFrame()));
                }
                CurrentFrame = 0;
            }
        }

        public override void Update(double elapsedTime)
        {
            _elapsedTime = elapsedTime;

            //Move the sprite
            Location.X += Velocity.X * (float)elapsedTime;
            Location.Y += Velocity.Y * (float)elapsedTime;

            Angle += AngularVelocity * (float)elapsedTime;

        }

        public override void Draw(Graphics graphics)
        {
            Matrix m = new Matrix();
            m.RotateAt(Angle, new PointF(Location.X + Size.Width / 2, Location.Y + Size.Height / 2));
            graphics.Transform = m;

            if (animated)
            {
                animTimer += 1000 * _elapsedTime;  //convert to milliseconds
                if (animTimer > 90) // advance frame every 90ms
                {
                    animTimer = 0;
                    advanceFrame();
                }
                graphics.DrawImage(_frames[CurrentFrame], 0, 40);
            }
            else
            {
                //Draw the correct frame at the current point
                graphics.DrawImage(_frames[0], Location.X, Location.Y, Size.Width, Size.Height);
            }
        }

        private Bitmap PaintFrame()
        {
            Bitmap temp;
            Graphics tempGraphics;

            temp = new Bitmap((int)Size.Width, (int)Size.Height);
            tempGraphics = Graphics.FromImage(temp);

            tempGraphics.DrawImageUnscaled(sprite, 0, 0 - (int)(Size.Height * CurrentFrame));

            tempGraphics.Dispose();
            return (temp);
        }

        private void advanceFrame()
        {
            CurrentFrame++;
            if (CurrentFrame >= sprite.Height / Size.Height)
                CurrentFrame = 0;
        }

        public static bool Collision(GameSprite sprite1, GameSprite sprite2)
        {
            //See if the sprite rectangles overlap
            return !(sprite1.Location.X > sprite2.Location.X + sprite2.Size.Width
                    || sprite1.Location.X + sprite1.Size.Width < sprite2.Location.X
                    || sprite1.Location.Y > sprite2.Location.Y + sprite2.Size.Height
                    || sprite1.Location.Y + sprite1.Size.Height < sprite2.Location.Y);
        }
    }
   
}
